{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 41 自然语言处理"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "1.【选择】请选出以下几个包中用于Python中文自然语言处理的包（）  \n",
    "\n",
    "A.NTLK  \n",
    "B.spaCy  \n",
    "C.Jieba  "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "答案：C"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "2.【填空】请写出利用自然语言处理工具pynlpir进行分词处理的函数（ ）"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "答案：pynlpir.segment()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "3.【选择】以下哪个函数可以利用自然语言处理工具pynlpir自定义词汇？（ ）  \n",
    "\n",
    "\n",
    "A.pynlpir.AddUserWord()  \n",
    "B.nlpir.AddUserWord()  \n",
    "C.pynlpir.get_key_words()  \n",
    "D.pynlpir.nlpir.AddUserWord() "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "答案：D"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "4.【填空】在下方横线处填写合适的函数，使词云的背景颜色为白色。  \n",
    "wc=WordCloud(font_path='C:\\Windows\\Fonts\\simhei.ttf',________,mask=mask,max_words=600, max_font_size=80,scale=20)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "答案：background_color=\"white\""
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "5.【编程】编写代码，绘制出十九大报告的词云。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": false
   },
   "source": [
    "答案：  \n",
    "`import pynlpir as pynlpir\n",
    "import numpy as np\n",
    "import pandas as pd\n",
    "import matplotlib.pyplot as plt\n",
    "import seaborn as sns; sns.set()\n",
    "from wordcloud import WordCloud,ImageColorGenerator\n",
    "from PIL import Image\n",
    "import jieba\n",
    "import collections\n",
    "% matplotlib inline\n",
    "text=open('十九大报告.txt','r').read().replace('\\n','').replace('\\u3000','')\n",
    "wordcloud = WordCloud().generate(text)\n",
    "seg_list=jieba.cut(text,cut_all=False)\n",
    "object_list = []\n",
    "remove_words=[u'为',u'中',u'上',u'到',u'对',u'以',u'是',u'的',u'把',u'了',u'在',u'要',u'和',u'，',u'、',u'。',u'“',u'”',u'‘',u'’',u'（',u'）',u'！']\n",
    "for word in seg_list:   \n",
    "    if word not in remove_words:\n",
    "        object_list.append(word) \n",
    "word_counts=collections.Counter(object_list)\n",
    "mask=np.array(Image.open('wordcloud.jpg'))\n",
    "pic=WordCloud(font_path='C:\\Windows\\Fonts\\simhei.ttf',background_color=\"white\",mask=mask,max_words=300, max_font_size=80)\n",
    "pic.generate_from_frequencies(word_counts)\n",
    "image_colors = ImageColorGenerator(mask)\n",
    "pic.recolor(color_func=image_colors) \n",
    "pic.to_file(\"E:/temp.jpg\")\n",
    "plt.imshow(pic)\n",
    "plt.axis('off')\n",
    "plt.show()`"
   ]
  }
 ],
 "metadata": {
  "anaconda-cloud": {},
  "kernelspec": {
   "display_name": "Python [default]",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.5.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
